package views;

import presenters.FilterPresenter;
import com.example.wheresmystuff.R;

import data.Session;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

/**
 * Activity to choose filters for item list
 * 
 * @author Kelsey
 */
public class FilterActivity extends Activity implements IFieldView {

	/** presenter for item filter */
	private FilterPresenter myPresenter;

	/** login session data */
	private Session session;

	private RadioButton radioOpenStatus;
	private RadioButton radioResolvedStatus;
	private RadioButton radioBothStatus;

	private CheckBox checkHeirloomCategory;
	private CheckBox checkKeepsakeCategory;
	private CheckBox checkMiscCategory;

	private RadioButton radioNewestDate;
	private RadioButton radioOldestDate;

	private EditText searchQuery;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter);

		myPresenter = new FilterPresenter(this);

		session = (Session) getApplicationContext();

		initForms();
	}

	/**
	 * set form items to inital values
	 */
	private void initForms() {
		radioOpenStatus = (RadioButton) findViewById(R.id.radioButton1);
		radioResolvedStatus = (RadioButton) findViewById(R.id.radioButton2);
		radioBothStatus = (RadioButton) findViewById(R.id.radioButton3);

		checkHeirloomCategory = (CheckBox) findViewById(R.id.checkBox1);
		checkKeepsakeCategory = (CheckBox) findViewById(R.id.checkBox2);
		checkMiscCategory = (CheckBox) findViewById(R.id.checkBox3);

		radioNewestDate = (RadioButton) findViewById(R.id.radioButton4);
		radioOldestDate = (RadioButton) findViewById(R.id.radioButton5);
		searchQuery = (EditText) findViewById(R.id.queryText);

		String status = session.getFilterStatus();
		if (status.equals("open")) {
			radioOpenStatus.setChecked(true);
			radioResolvedStatus.setChecked(false);
			radioBothStatus.setChecked(false);
		} else if (status.equals("resolved")) {
			radioOpenStatus.setChecked(false);
			radioResolvedStatus.setChecked(true);
			radioBothStatus.setChecked(false);
		} else if (status.equals("both")) {
			radioOpenStatus.setChecked(false);
			radioResolvedStatus.setChecked(false);
			radioBothStatus.setChecked(true);
		}

		if (session.getFilterHeirloomCategory())
			checkHeirloomCategory.setChecked(true);

		if (session.getFilterKeepsakeCategory())
			checkKeepsakeCategory.setChecked(true);

		if (session.getFilterMiscCategory())
			checkMiscCategory.setChecked(true);

		if (session.getFilterNewestDate()) {
			radioNewestDate.setChecked(true);
			radioOldestDate.setChecked(false);
		} else {
			radioNewestDate.setChecked(false);
			radioOldestDate.setChecked(true);
		}

		searchQuery.setText(session.getSearchQuery());
	}

	/**
	 * set filter to show open status
	 * 
	 * @param source
	 *            the radio button
	 */
	public void onOpenStatusClick(View source) {
		session.setFilterStatus("open");
		radioOpenStatus.setChecked(true);
		radioResolvedStatus.setChecked(false);
		radioBothStatus.setChecked(false);
	}

	/**
	 * set filter to show resolved status
	 * 
	 * @param source
	 *            the radio button
	 */
	public void onResolvedStatusClick(View source) {
		session.setFilterStatus("resolved");
		radioOpenStatus.setChecked(false);
		radioResolvedStatus.setChecked(true);
		radioBothStatus.setChecked(false);
	}

	/**
	 * set filter to show both status
	 * 
	 * @param source
	 *            the radio button
	 */
	public void onBothStatusClick(View source) {
		session.setFilterStatus("both");
		radioOpenStatus.setChecked(false);
		radioResolvedStatus.setChecked(false);
		radioBothStatus.setChecked(true);
	}

	/**
	 * set filter to show heirlooms
	 * 
	 * @param source
	 *            the checkbox
	 */
	public void onHeirloomCategoryClick(View source) {
		if (session.getFilterHeirloomCategory())
			session.setFilterHeirloomCategory(false);
		else
			session.setFilterHeirloomCategory(true);
	}

	/**
	 * set filter to show keepsakes
	 * 
	 * @param source
	 *            the checkbox
	 */
	public void onKeepsakeCategoryClick(View source) {
		if (session.getFilterKeepsakeCategory())
			session.setFilterKeepsakeCategory(false);
		else
			session.setFilterKeepsakeCategory(true);
	}

	/**
	 * set filter to show misc items
	 * 
	 * @param source
	 *            the checkbox
	 */
	public void onMiscCategoryClick(View source) {
		if (session.getFilterMiscCategory())
			session.setFilterMiscCategory(false);
		else
			session.setFilterMiscCategory(true);
	}

	/**
	 * set filter to show newest items first
	 * 
	 * @param source
	 *            the radio button
	 */
	public void onNewestDateClick(View source) {
		session.setFilterNewestDate(true);
		radioNewestDate.setChecked(true);
		radioOldestDate.setChecked(false);
	}

	/**
	 * set filter to show oldest items first
	 * 
	 * @param source
	 *            the radio button
	 */
	public void onOldestDateClick(View source) {
		session.setFilterNewestDate(false);
		radioNewestDate.setChecked(false);
		radioOldestDate.setChecked(true);
	}

	/**
	 * when back button is clicked
	 * 
	 * @param source
	 *            back button
	 */
	public void onBackClick(View source) {
		session.setSearchQuery(searchQuery.getText().toString());
		myPresenter.onBackClick(source);
	}

	/**
	 * advances to the next source
	 * 
	 * @param source
	 *            - the source
	 */
	@Override
	public void advance(View source) {
		Intent intent;
		switch (source.getId()) {
		case R.id.button1: // back
			intent = new Intent(this, MainListActivity.class);
			startActivity(intent);
			finish();
		}
	}

	/**
	 * shows the failure
	 * 
	 * @param toast
	 *            - the toast message
	 */
	@Override
	public void showToast(String toast) {

	}

	/**
	 * hides the soft keyboard
	 * 
	 * @param source
	 *            - the source
	 */
	@Override
	public void hideSoftKeyboard(View source) {

	}

	/**
	 * Shows a progress bar
	 */
	@Override
	public void showProgressBar() {

	}

}
